package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import java.util.Collection;
import com.lb.jwitter.util.ApplicationContextProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblDKMHCamHoacQuyDinh;
import com.lb.jwitter.service.TblDKMHCamHoacQuyDinhService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblDKMHCamHoacQuyDinhUpdateAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private int dotDKId;

	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	public int getDotDKId() {
		return dotDKId;
	}

	private TblDKMHDotDKService tblDKMHDotDKService;

	private int hocVienId;

	public void setHocVienId(int hocVienId) {
		this.hocVienId = hocVienId;
	}

	public int getHocVienId() {
		return hocVienId;
	}

	private TblThongTinHocVienService tblThongTinHocVienService;

	private int monHocId;

	public void setMonHocId(int monHocId) {
		this.monHocId = monHocId;
	}

	public int getMonHocId() {
		return monHocId;
	}

	private TblCTDTMonHocService tblCTDTMonHocService;

	private String nhomHoc;

	public void setNhomHoc(String nhomHoc) {
		this.nhomHoc = nhomHoc;
	}

	public String getNhomHoc() {
		return nhomHoc;
	}

	private String maTinhNang;

	public void setMaTinhNang(String maTinhNang) {
		this.maTinhNang = maTinhNang;
	}

	public String getMaTinhNang() {
		return maTinhNang;
	}

	private int canBoId;

	public void setCanBoId(int canBoId) {
		this.canBoId = canBoId;
	}

	public int getCanBoId() {
		return canBoId;
	}

	private TblCanBoService tblCanBoService;

	private String ngayCapNhat;

	public void setNgayCapNhat(String ngayCapNhat) {
		this.ngayCapNhat = ngayCapNhat;
	}

	public String getNgayCapNhat() {
		return ngayCapNhat;
	}
	
	private String maMonHoc;
	public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}

	private String mssv;
  public String getMssv() {
		return mssv;
	}

	public void setMssv(String mssv) {
		this.mssv = mssv;
	}

	private TblDKMHCamHoacQuyDinh tblDKMHCamHoacQuyDinh;

	public void setTblDKMHCamHoacQuyDinh(
			TblDKMHCamHoacQuyDinh tblDKMHCamHoacQuyDinh) {
		this.tblDKMHCamHoacQuyDinh = tblDKMHCamHoacQuyDinh;
	}

	private TblDKMHCamHoacQuyDinhService tblDKMHCamHoacQuyDinhService;

	private int preUpdate(Message msg, TblDKMHCamHoacQuyDinh obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postUpdate(Message msg, TblDKMHCamHoacQuyDinh obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService
					.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(),
							"tbl_DKMH_CamHoacQuyDinh", "sua")) {
				return "noprivilege";
			}
			tblDKMHCamHoacQuyDinhService = (TblDKMHCamHoacQuyDinhService) context.getBean("tbl_DKMH_CamHoacQuyDinhClient");
			tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");

			tblThongTinHocVienService = (TblThongTinHocVienService) context.getBean("tbl_ThongTin_HocVienClient");

			tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient");
			tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient");

			msg = new Message();
			
			//get mon hoc theo ma mon hoc
	        List<ConditionReport> cons = new ArrayList<ConditionReport>();
	        {
	        	ConditionReport con = new ConditionReport();
	        	con.setCol("ma");
	        	con.setValue1(maMonHoc);
	        	con.setOperator("eq");
	        	cons.add(con);
	        }
	        
	        List<TblCTDTMonHoc> monHoc = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 1, 1);
	        
	        if(monHoc == null || monHoc.size() <1){
	        	return Action.ERROR;
	        }
	        tblDKMHCamHoacQuyDinh.setMonHocId(monHoc.get(0));
	        
	        cons.clear();
	        {
	        	ConditionReport con = new ConditionReport();
	        	con.setCol("MSSV");
	        	con.setValue1(mssv);
	        	con.setOperator("eq");
	        	cons.add(con);
	        }
	        List<TblThongTinHocVien> hocVien = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 1, 1);
	        if(hocVien == null || hocVien.size() <1){
	        	return Action.ERROR;
	        }
	        tblDKMHCamHoacQuyDinh.setHocVienId(hocVien.get(0));
	        
			tblDKMHCamHoacQuyDinh = tblDKMHCamHoacQuyDinhService.gettblDKMHCamHoacQuyDinh(new Long(id));
			tblDKMHCamHoacQuyDinh.setNhomHoc(nhomHoc);
			tblDKMHCamHoacQuyDinh.setMaTinhNang(maTinhNang);
			tblDKMHCamHoacQuyDinh.setCanBoId(taiKhoanDangNhap.getCanBoId());

			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			Date dd = new Date();
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(dd);
			XMLGregorianCalendar d = DatatypeFactory.newInstance()
					.newXMLGregorianCalendar(c);

			tblDKMHCamHoacQuyDinh.setNgayCapNhat(d);
			preUpdate(msg, tblDKMHCamHoacQuyDinh);
			if (msg.isPreMessage_b()) {
				tblDKMHCamHoacQuyDinhService
						.updatetblDKMHCamHoacQuyDinh(tblDKMHCamHoacQuyDinh);
				postUpdate(msg, tblDKMHCamHoacQuyDinh);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}

			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Update tbl_DKMH_CamHoacQuyDinh Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
